package cn.sdormitory.school.service.impl;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.school.dao.SchoolPatrolDao;
import cn.sdormitory.school.dao.SchoolPatrolHandleDao;
import cn.sdormitory.school.entity.SchoolPatrol;
import cn.sdormitory.school.entity.SchoolPatrolHandle;
import cn.sdormitory.school.service.SchoolPatrolHandleService;
import cn.sdormitory.school.vo.PatrolHandleQueryVo;
import cn.sdormitory.school.vo.SchoolPatrolVo;
import cn.sdormitory.sys.dao.SysDictDetailDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SchoolPatrolHandleServiceImpl extends ServiceImpl<SchoolPatrolHandleDao, SchoolPatrolHandle>
                                    implements SchoolPatrolHandleService {

    @Autowired
    SchoolPatrolDao schoolPatrolDao;

    @Autowired
    SchoolPatrolHandleDao schoolPatrolHandleDao;

    @Autowired
    SysUserDao sysUserDao;

    @Autowired
    BClassDao bClassDao;

    @Autowired
    SysDictDetailDao sysDictDetailDao;

    @Override
    public List<SchoolPatrolVo> findToDoListByTeacherId(Long chargeTeacherId) {
        List<SchoolPatrolHandle> toDoPatrolHandleList = schoolPatrolHandleDao.findToDoListByTeacherId(chargeTeacherId);
        List<SchoolPatrolVo> patrolList = new ArrayList<>();
        for(SchoolPatrolHandle schoolPatrolHandle : toDoPatrolHandleList){
            SchoolPatrolVo schoolPatrol = schoolPatrolDao.findById(schoolPatrolHandle.getPatrolId());
            patrolList.add(schoolPatrol);
        }
        return patrolList;
    }

    @Override
    public Integer findToDoListCountByTeacherId(Long chargeTeacherId) {
        return schoolPatrolHandleDao.findToDoListCountByTeacherId(chargeTeacherId);
    }

    @Override
    public int updatePatrolHandleStatus(Map<String,Object> params){
        Long patrolId = Long.valueOf(params.get("patrolId") + "");
        String handleInfo = (String)params.get("handleInfo");
        String actualHandler = (String)params.get("actualHandler");
        String handleImg = (String)params.get("handleImg");
        SchoolPatrolHandle schoolPatrolHandle = schoolPatrolHandleDao.findByPatrolId(patrolId);
        schoolPatrolHandle.setHandleInfo(handleInfo);
        schoolPatrolHandle.setActualHandler(actualHandler);
        schoolPatrolHandle.setHandleImg(handleImg);
        schoolPatrolHandle.setHandleStatus("1");
        schoolPatrolHandle.setHandleDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return schoolPatrolHandleDao.updateById(schoolPatrolHandle);
    }

    @Override
    public IPage<SchoolPatrolHandle> findPageList(PatrolHandleQueryVo vo) {
        int count = schoolPatrolHandleDao.findListCount(vo);
        int startNum = (vo.getPageNum() - 1) * vo.getPageSize();
        List<SchoolPatrolHandle> list = schoolPatrolHandleDao.findPageList(startNum,vo.getPageSize(),vo);
        IPage<SchoolPatrolHandle> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> findList(Map<String,Object> params) {
        Long userId = Long.valueOf(params.get("userId") + "");
        String startDate = (String)params.get("startDate");
        String endDate = (String)params.get("endDate");
        String patrolType = (String)params.get("patrolType");

        if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            startDate = format.format(date);
            endDate = format.format(date);
        }

        //查询指定日期范围内所有数据
        List<SchoolPatrolHandle> patrolHandleList = schoolPatrolHandleDao.findByDate(startDate,endDate,patrolType);

        //声明返回变量
        List<Map<String, Object>> list = new ArrayList<>();

        //查询当前登录用户部门
        SysUser sysUser = sysUserDao.selectById(userId);
        String deptName = sysUser.getDeptName();

        for(SchoolPatrolHandle item : patrolHandleList){
            SchoolPatrolVo schoolPatrol = schoolPatrolDao.findById(item.getPatrolId());
            Map<String, Object> map = new HashMap<>();
            if(!"校办".equals(deptName)){
                //班级，当前登录部门经理只能查看自己所属部门下的班级数据
                if(!("1".equals(schoolPatrol.getPatrolType()))){
                    String classId = schoolPatrol.getClassId();
                    BClass bClass = bClassDao.selectById(classId);
                    QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
                    wrapper.eq("dict_type","class_dept").eq("dict_value",bClass.getClassDept());
                    SysDictDetail sysDictDetail = sysDictDetailDao.selectOne(wrapper);
                    if(sysDictDetail.getDictLabel().equals(deptName)){
                        map.put("patrol",schoolPatrol);
                        map.put("handle",item);
                        list.add(map);
                    }
                //宿舍
                }else {
                    if("后勤部".equals(deptName)){
                        map.put("patrol",schoolPatrol);
                        map.put("handle",item);
                        list.add(map);
                    }
                }
            }else {
                map.put("patrol",schoolPatrol);
                map.put("handle",item);
                list.add(map);
            }
        }
        return list;
    }


}
